package com.shanghaionstar.user.service;

import android.content.Context;

import com.onstar.cn.api.UsersService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.mag.enumtype.OSType;
import com.onstar.cn.more.response.GetCardHistoryResponse;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by Mengzhou Gu on 2016/8/3.
 */
public class ActiveHistoryService extends BaseService<GetCardHistoryResponse> {
    private Context context;

    public ActiveHistoryService(Context context) {
        this.context = context;
    }

    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public GetCardHistoryResponse jsonToBean(String json) {
        return GsonHelper.fromJson( json,GetCardHistoryResponse.class);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.ZH), method = HttpMethod.GET)
    public Observable<GetCardHistoryResponse> getObserval(final int currentPage) {
        return Observable.defer(new Func0<Observable<GetCardHistoryResponse>>() {
            @Override
            public Observable<GetCardHistoryResponse> call() {
                try {
                    UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
                    String idpID = ApplicationUtil.getInstance().getIdpID();
                    String accountID = ApplicationUtil.getInstance().getAccountId();
                    String vin = ApplicationUtil.getInstance().getVin();
                    RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
                    requestEntity = service.getHistory(requestEntity, idpID, accountID, vin, OSType.ANDROID_SMALL.toString());
                    requestEntity =  HttpInjectUtils.injectHttp(ActiveHistoryService.this.getClass(), "getObserval", requestEntity,int.class);

                    return Observable.just( GsonHelper.fromJson(BusinessUtil.getResponse(getDatas(requestEntity), context),GetCardHistoryResponse.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });

    }

}
